﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Apex.Common.Base;
using Apex.Common.Models;
using Apex.Script.Models;
using Apex.Script.ViewModels;
using Elysium;
using Script.Components.Controllers;
using Script.Components.Windows;
using Skript = Apex.Script.Models.Script;

namespace Script
{
    public partial class MainWindow : Elysium.Controls.Window, INotifyPropertyChanged
    {
        #region Fields

        private object _currentContext;

        #endregion

        #region Properties

        public ScriptViewModel CurrentScript
        {
            get { return App.ScriptController.CurrentScript; }
        }

        public object CurrentDataContext
        {
            get { return _currentContext; }
            set { _currentContext = value; OnPropertyChanged("CurrentDataContext"); }
        }

        public bool IsBackButtonEnabled
        {
            get { return App.NavController.BackList.Count != 0; }
        }
        public bool IsForwardButtonEnabled
        {
            get { return App.NavController.ForwardList.Count != 0; }
        }

        #endregion

        #region Initialization

        public MainWindow()
        {
            SetIsMainWindow(this, true);
            CurrentDataContext = CurrentScript;
            InitializeComponent();
        }

        #endregion

        #region ListView Handlers

        private void btnOpenContent_Click(object sender, RoutedEventArgs e)
        {
            FrameworkElement btn = sender as FrameworkElement;
            var obj = btn.Tag;
            if (obj is SequenceGroupViewModel || obj is SequenceViewModel || obj is SceneViewModel)
            {
                App.NavController.AddBack(CurrentDataContext);
                CurrentDataContext = obj;
                OnPropertyChanged("IsBackButtonEnabled");
                OnPropertyChanged("IsForwardButtonEnabled");
            }
        }

        private void btnGoToMap_Click(object sender, RoutedEventArgs e)
        {
            var data = (sender as Hyperlink).Tag as LocationViewModel;
            MessageBox.Show(data.Position.ToString());
        }

        #endregion

        #region NavBar

        private void btnHome_Click(object sender, RoutedEventArgs e)
        {
            if (CurrentDataContext == CurrentScript)
                return;

            App.NavController.AddBack(CurrentDataContext);
            CurrentDataContext = CurrentScript;
        }

        private void btnBack_Click(object sender, RoutedEventArgs e)
        {
            CurrentDataContext = App.NavController.GetBack();
            OnPropertyChanged("IsBackButtonEnabled");
            OnPropertyChanged("IsForwardButtonEnabled");
        }

        private void btnForward_Click(object sender, RoutedEventArgs e)
        {
            CurrentDataContext = App.NavController.GetForward();
            OnPropertyChanged("IsBackButtonEnabled");
            OnPropertyChanged("IsForwardButtonEnabled");
        }

        private void btnAdd_Click(object sender, RoutedEventArgs e)
        {

        }

        private void btnEdit_Click(object sender, RoutedEventArgs e)
        {

        }

        private void btnRemove_Click(object sender, RoutedEventArgs e)
        {

        }

        #endregion

        #region INPC

        public event PropertyChangedEventHandler PropertyChanged;

        protected void OnPropertyChanged(string propertyName)
        {
            if (string.IsNullOrEmpty(propertyName))
                throw new ArgumentException("Property name must not be null or empty.");

            if (PropertyChanged == null)
                return;

            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }

        #endregion

        #region SideBar

        private void btnCharacters_Click(object sender, RoutedEventArgs e)
        {
            CharactersWindow characterWindow = new CharactersWindow();
            characterWindow.ShowDialog();
        }

        private void btnLocations_Click(object sender, RoutedEventArgs e)
        {
            LocationsWindow locationWindow = new LocationsWindow(CurrentScript);
            locationWindow.ShowDialog();
        }

        #endregion

    }
}
